package com.opensource.svgaplayer.entities;

import com.opensource.svgaplayer.proto.FrameEntity;
import com.opensource.svgaplayer.proto.SpriteEntity;
import com.opensource.svgaplayer.utils.Intrinsics;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class SVGAVideoSpriteEntity {
    private String imageKey;
    private String matteKey;
    private List<SVGAVideoSpriteFrameEntity> frames = new ArrayList();

    /**
     * getImageKey
     *
     * @return String
     */
    public String getImageKey() {
        return this.imageKey;
    }

    /**
     * getMatteKey
     *
     * @return String
     */
    public String getMatteKey() {
        return this.matteKey;
    }

    /**
     * getFrames
     *
     * @return List<SVGAVideoSpriteFrameEntity>
     */
    public List<SVGAVideoSpriteFrameEntity> getFrames() {
        return this.frames;
    }

    /**
     * SVGAVideoSpriteEntity
     *
     * @param obj ZSONObject
     */
    public SVGAVideoSpriteEntity(ZSONObject obj) {
        Intrinsics.checkParameterIsNotNull(obj, "obj");
        this.imageKey = obj.getString("imageKey");
        this.matteKey = obj.getString("matteKey");
        List<SVGAVideoSpriteFrameEntity> mutableFrames = new ArrayList();
        ZSONArray ZSONArray = obj.getZSONArray("frames");
        if (ZSONArray != null) {
            for (int i = 0; i < ZSONArray.size(); i++) {
                ZSONObject zSONObject = ZSONArray.getZSONObject(i);
                if (zSONObject != null) {
                    SVGAVideoSpriteFrameEntity frameEntity = new SVGAVideoSpriteFrameEntity(zSONObject);
                    List shapes = frameEntity.getShapes();
                    if (!shapes.isEmpty()) {
                        SVGAVideoShapeEntity svgaVideoShapeEntity = (SVGAVideoShapeEntity) frameEntity.getShapes().get(0);
                        if (svgaVideoShapeEntity.isKeep() && mutableFrames.size() > 0) {
                            mutableFrames.get(mutableFrames.size() - 1).getShapes();
                            frameEntity.setShapes(mutableFrames.get(mutableFrames.size() - 1).getShapes());
                        }
                    }
                    mutableFrames.add(frameEntity);
                }
            }
        }
        this.frames = mutableFrames;
    }

    /**
     * SVGAVideoSpriteEntity
     *
     * @param obj SpriteEntity
     */
    public SVGAVideoSpriteEntity(SpriteEntity obj) {
        Intrinsics.checkParameterIsNotNull(obj, "obj");
        this.imageKey = obj.imageKey;
        this.matteKey = obj.matteKey;
        SVGAVideoSpriteFrameEntity lastFrame = null;
        List<SVGAVideoSpriteFrameEntity> temp = new ArrayList<>();
        List<FrameEntity> frameEntities = obj.frames;
        if (frameEntities != null && frameEntities.size() > 0) {
            for (FrameEntity frameEntity : frameEntities) {
                SVGAVideoSpriteFrameEntity frameItem = new SVGAVideoSpriteFrameEntity(frameEntity);
                List<SVGAVideoShapeEntity> shapes = frameItem.getShapes();
                if (!shapes.isEmpty()) {
                    SVGAVideoShapeEntity svgaVideoShapeEntity = frameItem.getShapes().get(0);
                    if (svgaVideoShapeEntity != null) {
                        if (svgaVideoShapeEntity.isKeep()) {
                            if (lastFrame != null) {
                                frameItem.setShapes(lastFrame.getShapes());
                            }
                        }
                    }
                }
                lastFrame = frameItem;
                temp.add(frameItem);
            }
        }
        frames = temp;
    }
}
